﻿using Microsoft.Win32;
using System;
using System.Windows;
using System.Windows.Controls;
using FixtureManageSystem.Models;
using System.Threading;
using System.Threading.Tasks;
using System.Data.SqlClient;
using Log.Log;
using OfficeOpenXml;
using System.IO;
using CsvHelper;
using System.ComponentModel;


namespace FixtureManageSystem.Views
{
    /// <summary>
    /// DataExport.xaml 的交互逻辑
    /// </summary>
    public partial class DataExport : Page
    {
        SaveFileDialog dlg;
        string TableType;
        public DataExport()
        {
            InitializeComponent();
        }
        private void button_Click(object sender, RoutedEventArgs e)
        {
            dlg = new SaveFileDialog();
            dlg.Title = "保存文件到";
            dlg.FileName = $"{comboBox.Text}数据.csv";
            dlg.DefaultExt = ".csv";
            dlg.Filter = "Text documents|*.csv";
            TableType = comboBox.Text;
            if (dlg.ShowDialog() == true)
            {
                Console.WriteLine(dlg.FileName);
            }
            else
            {
                return;
            }

            BackgroundWorker worker = new BackgroundWorker();
            worker.WorkerReportsProgress = true;
            worker.DoWork += woker_DoWork;
            worker.ProgressChanged += worker_ProgressChanged;
            worker.RunWorkerAsync();

        }

        void woker_DoWork(object sender, DoWorkEventArgs e)
        {
            DataExportService des = new DataExportService();

            des.Export(dlg.FileName, TableType, (BackgroundWorker)sender);
        }
 
        void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            progressBar.Value = e.ProgressPercentage;
        }


    }
}
